package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.permission;

import com.alibaba.fastjson.JSON;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.SystemPlatformMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Menu;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RedisValue;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.SystemPlatform;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmPermissionService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmSystemPlatformService;
import com.kingyun.gpsinspection.purificationservice.services.redisservice.RedisClientTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by huangxueqian on 2017/1/10/010.
 * 平台子系统服务实现类
 */

public class JsmSystemPlatformServiceImpl implements JsmSystemPlatformService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SystemPlatformMapper systemPlatformMapper;
    @Value("#{configProperties['system.code']}")
    private String systemCode;
    @Autowired
    private RedisClientTemplate redisClientTemplate;
    @Autowired
    private JsmPermissionService permissionService;

    /**
     * 返回平台服务子系统
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<SystemPlatform> selectSystemPlatform(String pfToken) throws Exception {
        List<SystemPlatform> list = new ArrayList<>();
        String platformStr = redisClientTemplate.get(pfToken + "SystemPlatform");
        if (StringUtils.isEmpty(platformStr)) {
            list = systemPlatformMapper.selectSystemPlatformList();
            redisClientTemplate.set(pfToken + "SystemPlatform", JSON.toJSONString(list));
        } else {
            list = JSON.parseArray(platformStr, SystemPlatform.class);
        }
        return list;
    }

    /**
     * 根据条件查询ID集合
     *
     * @return
     */
    @Override
    public List<String> selectByListId(List<SystemPlatform> list) {
        return systemPlatformMapper.selectByListId(list);
    }

    /**
     * 查询所有子系统
     */
    @Override
    public List<SystemPlatform> selectAllSystemPlatform() throws Exception {
        List<SystemPlatform> list = systemPlatformMapper.selectSystemPlatformList();
        return list;
    }

    /**
     * @param systemCode
     * @return 根据系统标识查询所属子系统名称
     * @throws Exception
     */
    @Override
    public SystemPlatform selectSystemPlatformBySubSystem(String systemCode) throws Exception {
        return systemPlatformMapper.selectSystemPlatform(systemCode);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<SystemPlatform> list) {
        return systemPlatformMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<SystemPlatform> list) {
        return systemPlatformMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量删除
     * @Date 2017/2/8 17:17
     */
    @Override
    public int deleteList(List<SystemPlatform> list) {
        return systemPlatformMapper.deleteList(list);
    }

    /**
     * 根据token获取当前人的菜单相关的子系统
     *
     * @param pfToken
     * @param s
     * @return 返回平台子系统
     */
    @Override
    public List<SystemPlatform> selectSystemPlatform(String pfToken, String s) {
        List<SystemPlatform> list = new ArrayList<>();
        String platformStr = redisClientTemplate.get(pfToken + "SystemPlatform");
        if (StringUtils.isEmpty(platformStr)) {
            EmployeeVo employeeVo = this.employeeVo(pfToken);
            try {
                // 获取全量子系统信息
                list = systemPlatformMapper.selectSystemPlatformList();
                // 根据systemCode 循环获取有菜单的子系统
                Iterator<SystemPlatform> SystemPlatforms = list.iterator();
                while (SystemPlatforms.hasNext()) {
                    SystemPlatform systemPlatform = SystemPlatforms.next();
                    // 系统编码
                    String systemCode = systemPlatform.getSystemCode();
                    List<Menu> firstMenus = permissionService.getFirstNavPermission(systemCode, employeeVo, pfToken);
                    // 如果根据系统编码没查询到菜单，就从全量子系统中移除
                    if (StringUtils.isEmpty(firstMenus) || firstMenus.size() == 0) {
                        SystemPlatforms.remove();
                    }
                }
                redisClientTemplate.set(pfToken + "SystemPlatform", JSON.toJSONString(list));
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            list = JSON.parseArray(platformStr, SystemPlatform.class);
        }
        return list;
    }

    /**
     * 根据token获取存入redis的用户数据
     *
     * @param pfToken
     * @return
     */
    private EmployeeVo employeeVo(String pfToken) {
        if (StringUtils.isEmpty(pfToken)) {
            return null;
        }
        RedisValue redisValue = null;
        if (redisClientTemplate.exists(pfToken)) {
            try {
                String value = redisClientTemplate.get(pfToken);
                redisValue = JSON.parseObject(value, RedisValue.class);
                logger.info("人员鉴权令牌:{},redis获取的数据:{}", pfToken, redisValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        EmployeeVo employeeVo = null;
        if (redisValue != null) {
            employeeVo = redisValue.getEmployeeVo();
        }
        return employeeVo;
    }

    @Override
    public SystemPlatform selectByPrimaryKey(String systemPlatformId) {
        // 该方法仅用于根据查询当前的系统编号
        return systemPlatformMapper.selectSystemPlatform(systemPlatformId);
    }
}
